package koction.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import koction.model.product.ProductService;
import koction.model.product.ProductVO;
import koction.model.reviewBoard.ListVO;
import koction.model.reviewBoard.ReviewBoardService;
import koction.model.reviewBoard.ReviewBoardVO;
import koction.model.seller.FileVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ReviewBoardController {
	@Resource(name="reviewBoardServiceImpl")
	private ReviewBoardService reviewBoardService;
	@Resource(name="productServiceImpl")
	private ProductService productService;
	/** upload path  path 마다 변경해줘야 함*/
	private String path="C:\\java-kosta\\final-project-workspace\\final-project-koction-basic\\WebContent\\reviewBoardUpload\\";
	
	@RequestMapping("/reviewBoard.do")
	public ModelAndView reviewBoardPage(int productId){
		System.out.println(productId);
		return new ModelAndView("writeReviewBoard.review","productId",productId);
	}
	
	/**
	 * 리뷰 게시판에 파일 업로드 추가
	 */
	@RequestMapping("/writeReviewBoard.do")
	public ModelAndView writeReviewBoard(ReviewBoardVO rvo,FileVO fvo) throws SQLException, IllegalStateException, IOException{
		reviewBoardService.writeReviewBoard(rvo);
		System.out.println(fvo);
		/* 파일 업로드*/
		MultipartFile mfile=fvo.getUploadFile();
		if(mfile.isEmpty()==false){
			//원본 파일명
			String fileName=mfile.getOriginalFilename();
			fvo.setOrgfilename(fileName);
			//
			String newFileName=rvo.getReviewNo()+".jpg";
			fvo.setNewfilename(newFileName);
			// 파일 업로드
			File uploadFile=new File(path+newFileName);
			mfile.transferTo(uploadFile);
		}
		return new ModelAndView("redirect:showContentReviewBoard.do?reviewNo="+rvo.getReviewNo());
	}
	
	@RequestMapping("/deleteReviewBoard.do")
	public ModelAndView deleteReviewBoard(int no, String pageNo, String productId){
		reviewBoardService.deleteReviewBoard(no);
		ProductVO pvo=productService.getProductInfo(productId);
		ListVO lvo=reviewBoardService.getReviewBoardListPaging("1", productId);
		HashMap map=new HashMap();
		map.put("lvo", lvo);
		map.put("info", pvo);
		return new ModelAndView("productInfo.product","map",map);
	}
	
	@RequestMapping("/updateReviewBoardView.do")
	public ModelAndView updateReviewBoardView(int reviewNo){
		ReviewBoardVO rvo=reviewBoardService.updateReviewBoardView(reviewNo);
		return new ModelAndView("updateReviewBoardView.review","rvo",rvo);
	}
	
	@RequestMapping("/updateReviewBoard.do")
	public ModelAndView updateReviewBoard(ReviewBoardVO rvo){
		reviewBoardService.updateReviewBoard(rvo);
		return new ModelAndView("showContentReviewBoard.review","rvo",rvo);
	}
	
	@RequestMapping("/showContentReviewBoard.do")
	public ModelAndView showContentReviewBoard(HttpSession session,ReviewBoardVO rvo, int reviewNo) throws SQLException{
		// productinfo.jsp 에서 요청된 정보이면 flag true 이므로 조회수 증가한다. 
		if(session.getAttribute("flag")==null){
			rvo=reviewBoardService.showContentReviewBoardNoHit(reviewNo);
		} else {
			rvo=reviewBoardService.showContentReviewBoard(reviewNo);
			session.removeAttribute("flag");
		}
		
		return new ModelAndView("showContentReviewBoard.review","rvo",rvo);
	}

}
